using Greg.XmlEditor.Business;
using Greg.XmlEditor.Model;

namespace Greg.XmlEditor.Presentation.Presenters
{
	public class DocumentPresenter : DocumentPresenterBase
	{
		private readonly IDocumentService documentService;
		private readonly Document document;

		public DocumentPresenter(Document document, IDocumentService documentService) 
			: base(document)
		{
			this.documentService = documentService;
			this.document = document;

			if (string.IsNullOrEmpty(this.Name)) this.Name = "Document" + document.Id;
			this.Schema = new SchemaPresenter(document.Schema, this, documentService);
			this.Style = new StylePresenter(document.Style, this, documentService);
		}

		#region Bindable properties

		public SchemaPresenter Schema { get; private set; }
		public StylePresenter Style { get; private set; }

		#endregion

		public override void Open(string fileName)
		{
			this.documentService.Load(this.document, fileName);
			this.RefreshProperties();
		}

		public override void Save(string fileName)
		{
			this.document.Text = this.Text;
			this.documentService.Save(this.document, fileName);
			this.FullPath = fileName;
			this.RefreshProperties();
		}
	}
}